
(function(w) {
    var isIctInit = false;
    var ict = {
        init: function() {
            if (isIctInit) {
                return;
            }
            ict.initPopupClose();
            isIctInit = true;
        },
        /**
         * 异步加载图片，对设置了 class 为 lazy，并且 data-original 的进行异步加载
         * <img class="lazy" data-original="http://img.blog.csdn.net/20171025113925914" >
         */
        lazyLoadImg: function() {
            $('.lazy').each(function(){
                try {
                    var imgOrig = $(this).attr('data-original');
                    var srcImg = $(this).attr('src');
                    if ((typeof(srcImg)=="undefined" || srcImg == null || srcImg.length == 0) && imgOrig != null && imgOrig.length > 0) {
                        $(this).attr('src', imgOrig);
                    }
                } catch (error) {
                }
                
            });
        },
        /**
         * 获取url后面的参数
         */
        getParam: function(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = decodeURI(window.location.search).substr(1).match(reg);
            if (r != null)
                return unescape(r[2]);
            return null;
        },
        isPhone: function(num) {
            if (!/^1\d{10}$/.test(num)) {
                return false;
            }
            return true;
        },
        log: function(msg) {
            try {
                if (typeof(console) != 'undefined')
                    console.log(msg);
            } catch (error) {
            }
        },

        /**
         * 
         * @param {*} cname 
         * @param {*} cvalue 
         */
        setCookie: function(cname, cvalue) {
            setCookie2(cname, cvalue, 1);
        },
        /**
         * 设置cookie信息
         * @param {*} cname 
         * @param {*} cvalue 
         * @param {*} exdays 
         */
        setCookie2: function(cname, cvalue, exdays) {
            if (isNaN(exdays) || exdays <= 0) {
                exdays = 1;
            }
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toGMTString();
            document.cookie = cname + "=" + cvalue + "; " + expires;
        },
        /**
         * 获取cookie信息
         * @param {*} cname 
         */
        getCookie: function(cname) {
            var arr = document.cookie.replace(/\s/g, "").split(';');
            for(var i=0; i < arr.length; i++) {
                var tempArr = arr[i].split('=');
                if(tempArr[0] == cname) {
                    return tempArr[1];
                }
            }
            return '';
        },
        /**
         * 删除cookie信息
         * @param {*} name 
         */
        delCookie: function(name) {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval = getCookie(name);
            if (cval != null)
                document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
        },
        //  cookie设置 结束

        // 去除空格
        trim: function(str, is_global) {
            var result;
            str = str + '';
            result = str.replace(/(^\s+)|(\s+$)/g, "");
            if (is_global.toLowerCase() == "g") {
                result = result.replace(/\s/g, "");
            }
            return result;
        },

        /**
         * 初始化弹窗的关闭按钮事件
         */
        initPopupClose: function() {
            $('body').on('click', '.popup .wrap .close', function() {
                $('.popup').hide();
                $('.popup .wrap').hide();
            });
        },

        /**
         * 打开跟着具体业务页面的遮罩弹窗
         * @param {*} id 
         */
        popup: function(id) {
            // $('.msk').hide();
            // $('.msk').children().hide();
            $(id).siblings().hide();
            $(id).show();
            $('.popup').show();
        },

        /**
         * 关闭跟着具体业务页面的遮罩弹窗
         * @param {*} id 
         */
        closepop: function(id) {
            if ($(id).css("display") != 'none') {
                $('.popup').hide();
                $(id).hide();
            }
        },
        toast: function(msg, time) {
            var msg = msg;
            $('.toastMsk .toast .msg').html(msg);
            $('.toastMsk').show();
            if (time == null || time == '' || time <= 0) {
                time = 2000;
            }
            setTimeout(function() {
                $('.toastMsk').hide();
            }, time);
        },

        post: function(url, data, func1, func2) {
            ict.ajax(url, data, "POST", func1, func2);
        },
        ajax: function(url, data, method, func1, func2) {
            var handle_error = function(jqXHR, textStatus) {
                try {
                    if (jqXHR.state() == "resolved") {
                        func2();
                        return;
                    }
                    if (jqXHR.status == 200 && jqXHR.state() == "rejected") {
                        return;
                    }
                    func2();
                } catch (error) {
                }
            };
            $.ajax({
                url: url,
                data: data,
                method: method,
                success: func1,
                error: handle_error,
                dataType: 'json',
            });
        },

    };

    w.ict = ict;
})(window);

/**
 * 判断终端浏览器
 * if (browser.versions.mobile || browser.versions.android || browser.versions.ios) {
 * 
 * }
 */
var browser = {
    versions: function() {
        var u = navigator.userAgent,
            app = navigator.appVersion;
        return {
            trident: u.indexOf('Trident') > -1, //IE内核
            presto: u.indexOf('Presto') > -1, //opera内核
            webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
            mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
            iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
            iPad: u.indexOf('iPad') > -1, //是否iPad
            webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
            weixin: u.indexOf('MicroMessenger') > -1, //是否微信 （2015-01-22新增）
            qq: u.match(/\sQQ/i) == " qq" //是否QQ
        };
    }(),
    language: (navigator.browserLanguage || navigator.language).toLowerCase()
}